package com.ljx.funchat.chat.controller;

import com.ljx.funchat.chat.domain.vo.req.ChatMessagePageReq;
import com.ljx.funchat.chat.domain.vo.req.ChatMessageReadReq;
import com.ljx.funchat.chat.domain.vo.req.ChatMessageReq;
import com.ljx.funchat.chat.domain.vo.resp.ChatMessageResp;
import com.ljx.funchat.chat.service.ChatService;
import com.ljx.funchat.common.annotation.frequency.FrequencyControl;
import com.ljx.funchat.common.domain.vo.resp.ApiResult;
import com.ljx.funchat.common.domain.vo.resp.CursorPageBaseResp;
import com.ljx.funchat.common.utils.RequestHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "聊天接口")
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @ApiModelProperty("发送消息")
    @PostMapping("/msg")
    @FrequencyControl(key = "sendMsg", count = 1, time = 500)
    public ApiResult<ChatMessageResp> sendMsg(@Valid @RequestBody ChatMessageReq chatMessageReq){
        Long uid = RequestHolder.get().getUid();

        return ApiResult.success(chatService.sendMsg(uid, chatMessageReq));
    }

    @ApiOperation("聊天列表")
    @GetMapping("/msg/page")
    public ApiResult<CursorPageBaseResp<ChatMessageResp>> getChatMsgPage(@Valid ChatMessagePageReq req){
        Long uid = RequestHolder.get().getUid();

        return ApiResult.success(chatService.getChatMsgPage(uid, req));
    }

    @ApiOperation("消息阅读")
    @PostMapping("/msg/read")
    public ApiResult<Void> readMsg(@Valid @RequestBody ChatMessageReadReq readReq){
        Long uid = RequestHolder.get().getUid();
        chatService.readMsg(uid, readReq);

        return ApiResult.success();
    }
}
